﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_ExpertAnswerQuestion : PX.MyPage
{
	/// <summary>
	/// 问题控制类
	/// </summary>
	/// <![CDATA[2014-7-21 17:26:00]]>
	private PX.Question.Component clsQuestonComp = new PX.Question.Component();

	protected void Page_Load(object sender, EventArgs e)
	{
		if (!IsPostBack)
		{
			BindGridView();
		}
	}

	protected override void OnInit(EventArgs e)
	{
		base.OnInit(e);

		clsQuestonComp.page = Page;

		Grv.RowCommand += new GridViewCommandEventHandler(Grv_RowCommand);
		LBtnClose.Click += new EventHandler(LBtnClose_Click);
		LBtnAnswer.Click += new EventHandler(LBtnAnswer_Click);
	}

	void LBtnAnswer_Click(object sender, EventArgs e)
	{
		PShade.Visible = false;
		PFloat.Visible = false;
		Answer();
		BindGridView();
	}

	void LBtnClose_Click(object sender, EventArgs e)
	{
		PShade.Visible = false;
		PFloat.Visible = false;
	}

	void Grv_RowCommand(object sender, GridViewCommandEventArgs e)
	{
		PShade.Visible = true;
		PFloat.Visible = true;

		ViewState["QuestionID"] = e.CommandArgument.ToString();
		ShowQuestionDetail();
	}
	/// <summary>
	/// 绑定未回答问题GridView控件
	/// </summary>
	/// <![CDATA[2014-7-21 16:45:40]]>
	private void BindGridView()
	{
		int nLoginUserID = 0;

		if (Session["LoginUserID"] != null)
		{
			nLoginUserID = Convert.ToInt32(Session["LoginUserID"]);

			Grv.DataSource = clsQuestonComp.QueryNoAnswerOffLine(nLoginUserID);
			Grv.DataKeyNames = new string[] { "ID" };
			Grv.DataBind();
		}
	}
	/// <summary>
	/// 显示一个问题的详细信息
	/// </summary>
	/// <![CDATA[2014-7-21 20:54:42]]>
	private void ShowQuestionDetail()
	{
		int nQuestionID;
		PX.Question.Entity clsQuestion = new PX.Question.Entity();

		nQuestionID = Convert.ToInt32(ViewState["QuestionID"]);

		clsQuestion = clsQuestonComp.GetOne(nQuestionID);

		if (clsQuestion != null)
		{
			TbQuestion.Text = clsQuestion.Content;
			
		}
	}
	/// <summary>
	/// 回答问题
	/// </summary>
	/// <![CDATA[2014-7-21 21:40:00]]>
	private void Answer()
	{
		int nQuestionID;
		int nAnswerID;
		PX.Question.Entity clsQuestion = new PX.Question.Entity();
		PX.Answer.Entity clsAnswer = new PX.Answer.Entity();
		PX.Answer.Component clsAnswerComp = new PX.Answer.Component();

		clsAnswerComp.page = Page;

		nQuestionID = Convert.ToInt32(ViewState["QuestionID"]);

		clsAnswer.QuestionID = nQuestionID;
		clsAnswer.Content = TbAnswer.Text;

		if (clsAnswerComp.Answer(clsAnswer))
		{
			nAnswerID = clsAnswerComp.GetNewestAddID();

			clsQuestion = clsQuestonComp.GetOne(nQuestionID);
			clsQuestion.AnswerID = nAnswerID;

			if (clsQuestonComp.Answer(clsQuestion))
			{
				Common.ShowMessageAlert.ShowMessage(Page, "回答问题成功！");
			}
			else
			{
				Common.ShowMessageAlert.ShowMessage(Page, "错误：把回答ID写入问题表出错！");
			}
		}
		else
		{
			Common.ShowMessageAlert.ShowMessage(Page, "把回答添加到数据库出错，请检查！");
		}
	}
}